clear
global pathData pathCode pathOutput dateDataImport dateModelData dateModelOutput server serverseed pathRawData

% set paths
pathCode='/Users/mohitkarnani/Documents';
pathData='/Users/mohitkarnani/Documents/Platform-Externalities/private_data/4_clean';
pathOutput=[pathCode '/Platform-Externalities/5_paper/Round 1/Paper']; 
        
ParulaColors=[0.2422,0.1504,0.6603
0.2803,    0.2782,    0.9221
0.2440,    0.4358,    0.9988
0.1540,    0.5902,    0.9218
0.0297,    0.7082,    0.8163
0.1938,    0.7758,    0.6251
0.5044,    0.7993,    0.3480
0.8634,    0.7406,    0.1596
0.9892,    0.8136,    0.1885
0.9769,    0.9839,    0.0805];

cd([pathCode '/Platform-Externalities/for_publication/code']);

dateDataImport ='2023_07_24'; runImportAdmin=1; % fix this unless you change source data
dateModelData  ='2023_07_24';  
dateModelOutput='2023_07_24';

% Colors
princeton = [1.00,0.56,0.00];
harvard   = [0.68,0.00,0.00];
uchile    = [0.13,0.13,0.44];
gray      = [0.75,0.75,0.75];

% Set seed
s = RandStream('mt19937ar','Seed',101);
RandStream.setGlobalStream(s);

% Set parula color grid
c = parula;

% Options 
runDescriptiveStats=1;
runEstimatePreferences=[0 0 0 0 0];
runEstimateAlpha=0;
exportToJulia = 1;
runCounterfactual = 0;
writeP=1;

%% Import data
%{
This script imports information about college options, college applications and enrollment as well as test score data.
This is stored in four tables:

 Options   : Higher education options available on platform.
 Students  : All registered students taking the test.
 Apps      : All platform applications submitted and their outcomes.
 Enrollment: All freshmen enrollment outcomes (on- and off-platform).
%}

if runImportAdmin==1
    % Import data on options, students, and choices (Apps,Enrollment)
    tempOptions = readtable([pathData '/options.csv']);
    tempStudents = readtable([pathData '/students_mrun.csv']);
    tempApps = readtable([pathData '/apps_mrun.csv']);
    tempEnrollment = readtable([pathData '/enrollgrad_mrun.csv']);

    % Adjust to skip years not relevant to analysis
    Options=tempOptions(ismember(tempOptions.proceso,2010:2012),:);
    Students=tempStudents(ismember(tempStudents.proceso,2010:2012),:);
    Apps=tempApps(ismember(tempApps.proceso,2010:2012),:);
    Enrollment=tempEnrollment(ismember(tempEnrollment.proceso,2010:2012),:);
    
    clear tempOptions tempStudents tempApps tempEnrollment
    save([pathData '/Data_' dateDataImport '.mat'],'Options','Students','Apps','Enrollment')
else
    load([pathData '/Data_' dateDataImport '.mat'],'Options','Students','Apps','Enrollment')
end

%% exports
if exportToJulia == 1
    exportMCMCstuff;
end

%% Pij
if writeP==1
    if exportToJulia ~= 1
        [Data,options,apps,students,enrollment]=setup_data([2010,2011,2012],Options,Students,Apps,Enrollment);
        %load([pathData '/SetupData_' dateDataImport '.mat'],'Data','options','students','apps','enrollment')
    end
    [P2010,P2011,P2012]=Pij(students,options);
    csvwrite([pathData '/P2010.csv'],P2010);
    csvwrite([pathData '/P2011.csv'],P2011);
    csvwrite([pathData '/P2012.csv'],P2012);
end
